package com.itcast.controller;

import com.itcast.domain.Book;
import com.itcast.service.BookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public ResponseEntity<?> save(@RequestBody Book book) {
        System.out.println("save...");
        boolean saved = bookService.save(book);
        return ResponseEntity.ok(saved); // Return HTTP 200 OK with the boolean result
    }

    @PutMapping
    public ResponseEntity<?> update(@RequestBody Book book) {
        boolean updated = bookService.update(book);
        return ResponseEntity.ok(updated); // Return HTTP 200 OK with the boolean result
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable int id) {
        boolean deleted = bookService.delete(id);
        return ResponseEntity.ok(deleted); // Return HTTP 200 OK with the boolean result
    }

    @GetMapping("/{id}")
    public ResponseEntity<Book> getById(@PathVariable int id) {
        Book book = bookService.getById(id);
        return ResponseEntity.ok(book); // Return HTTP 200 OK with the Book object
    }

    @GetMapping
    public ResponseEntity<List<Book>> getAll() {
        List<Book> list = bookService.getAll();
        return ResponseEntity.ok(list); // Return HTTP 200 OK with the list of Book objects
    }
}
